package search.users;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import server.Initialize;
import server.db.StmtHelper;
import server.json.FunctionalJSON;

import ajaxmethods.AjaxMethod;

public class GetServiceProviders implements AjaxMethod {

	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/json");
		try {
			JSONObject agentsObject = new JSONObject().put("htmlValue", getSPs(request.getParameter("searchserviceprovider"))).put("htmlSelector", "#agentWorkspace .scrollable .verticalscroll .items")
				.put("numobjects", new JSONObject().put("end",Math.min(4, this.getNumberSPs())).put("total", this.getNumberSPs())).put("postcall", "search.users.ServiceProviderProfile");
			if(agentsArray.length()>0 && ((JSONObject)agentsArray.get(0)).get("idserviceproviders")!=request.getSession().getAttribute("currobjectid"))
			{
				agentsObject.put("objectid", ((JSONObject)agentsArray.get(0)).get("idserviceproviders"));
				request.getSession().setAttribute("currobjectid", ((JSONObject)agentsArray.get(0)).get("idserviceproviders"));
			}
			agentsObject.write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private JSONArray agentsArray;
	
	public Integer getNumberSPs() {
		return agentsArray.length();
	}
	
	public StringBuffer getSPs(String parameter) {
		JSONArray jsonArr = getSPJSONArray(parameter);
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"section\">");
			for(int i = 0; i < jsonArr.length();i++)
			{
				if(i>0&&i%4==0)
					sb.append("</div><div class=\"section\">");
				JSONObject agent;
				try {
					agent = ((JSONObject)jsonArr.get(i));
					sb.append("<div class=\"item\"><div id=\"shortofficereport\"><h4 class=\"fullname\"><a onclick=\"doPost('search.users.ServiceProviderProfile',{id:"+agent.get("idserviceproviders")+"});\">"+agent.get("fullname")+"</a></h4>");
					sb.append("<div id=\"photo\"><img class=\"listViewImage\" src=\""+Initialize.appURL()+"/"+agent.get("image_url")+"\"></div>");
					sb.append("<div class=\"agentfields\">");
					sb.append("<div>"+agent.get("officename")+" - "+agent.get("officeaddress")+"</div><div>"+agent.get("officephone")+"</div></div></div></div>");
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		sb.append("</div>");
		return sb;
	}

	private JSONArray getSPJSONArray(String parameter) {
		try {
			agentsArray = FunctionalJSON.mapToJSONArray.f(StmtHelper.db("select idserviceproviders,fullname,officephone,mobile,fax,officename,officeaddress,emailaddress,image_url ")
				.addSql(" from serviceproviders where servicetype like '%"+parameter+"%'")
				.collectIntoMaps.unsafeGet());
			return agentsArray;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
